﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GraphicsFramework.Entity;

namespace GraphicsFramework.Support
{
    class Mouse
    {

        private Point2I position = new Point2I();
        private GraphicsFramework graphics;

        private bool snap;

        public delegate void MouseChangedDelegate();

        public Mouse(GraphicsFramework graphics, Point2I position)
        {
            this.graphics = graphics;
            this.position = position;
        }

        public Mouse Clone()
        {
            Mouse clone = new Mouse(graphics, new Point2I(position.X, position.Y));
            return clone;
        }

        /// <summary>
        /// Raw, unconverted coordinate coming from the canvas mouse events.
        /// </summary>
        public Point2I RawCoordinate
        {
            get { return position; }
        }

        /// <summary>
        /// Actual mouse coordinate with respect to the current graphics coordinate.
        /// </summary>
        public Point2F ActualCoordinate
        {
            get { return graphics.ToPoint(position); }
        }

        /// <summary>
        /// Snapped mouse coordinate with respect to the current graphics coordinate.
        /// Mouse is snapped base on the current SnapValue.
        /// </summary>
        public Point2F SnappedCoordinate
        {
            get 
            { 
                Point2F coord = graphics.ToPoint(position);
                coord.X = (float) Math.Round((int)Math.Round(coord.X / graphics.SnapValue.X) * graphics.SnapValue.X, graphics.Precision);
                coord.Y = (float) Math.Round((int)Math.Round(coord.Y / graphics.SnapValue.Y) * graphics.SnapValue.Y, graphics.Precision);
                return coord;
            }
        }

        /// <summary>
        /// Current working coordinate. 
        /// If Snap is true, then the WorkingCoordinate will be SnappedCoordinate, otherwise, it will be ActualCoordinate.
        /// </summary>
        public Point2F WorkingCoordinate
        {
            get 
            {
                if (snap) return SnappedCoordinate;
                return ActualCoordinate;
            }
        }

        public bool Snap
        {
            get { return snap; }
            set { snap = value; }
        }

    }
}
